# Create your views here.
from itertools import chain
from django.template.context import RequestContext,Context
from django.shortcuts import render_to_response, get_object_or_404
from Biblia.models import Livro, Capitulo, Versao, Versiculo
from Scope.models import *
from Advertiser.models import *
from BlogJuvenis.models import *
from Galerya.models import *
from Shopping.models import *
from IgrejaMaps.models import *

def homepage(request):
    mn = Noticia.objects.filter(manchete__icontains=1,publicar__icontains=1).order_by('-datacriacao')[:5]
    me = DataEvento.objects.filter(eventoNoticia__manchete__icontains=1, eventoNoticia__publicar__icontains=1).order_by('-eventoNoticia__datacriacao')
    mg = Galeria.objects.filter(manchete__icontains=1,publicar__icontains=1).order_by('-datacriacao')[:5]
    mb = NoticiaBlog.objects.filter(manchete__icontains=1,publicar__icontains=1).order_by('-datacriacao')[:2]
    manchete = chain(mn, me, mg, mb)

    listanoticia = Noticia.objects.filter(nomeCategoria__id = 1,publicar__icontains=1,manchete__icontains=0).order_by('-datacriacao')[:5]

    listaevento = DataEvento.objects.filter(eventoNoticia__publicar__icontains=1, eventoNoticia__manchete__icontains=0, dataevento__gte=datetime.now()).order_by('dataevento')[:8]
    listahumor = Noticia.objects.filter(nomeCategoria__id = 3,publicar__icontains=1,manchete__icontains=0).order_by('-datacriacao')[:8]
    listagaleria = Galeria.objects.filter(publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:5]
    listadownload = Download.objects.filter(publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:5]
    listavideo = Video.objects.filter(publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:8]
    listamusica = Noticia.objects.filter(nomeCategoria__id = 4,publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:4]
    listaartigo = Noticia.objects.filter(nomeCategoria__id = 5,publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:8]
    listablog = NoticiaBlog.objects.filter(publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:8]

    cantor_destaque = Cantor.objects.filter(publicar=True, manchete=True)
    cantores = Cantor.objects.all()[:5]
    letra_destaque = Letra.objects.filter(publicar=True, manchete=True)
    return render_to_response('principal/scope/index.html',locals(),context_instance=RequestContext(request))

def noticiainterna(request, slugcategoria, slugnoticia):
    noticias = get_object_or_404(Noticia, nomeCategoria__slugcategoria = slugcategoria, slugnoticia=slugnoticia)
    listaartigo = Noticia.objects.filter(nomeCategoria__id = 5,publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:3]
    listablog = NoticiaBlog.objects.filter(publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:4]
    if noticiainterna:
        if not request.session.get('noticia_cliques_%s' % slugnoticia):
            noticias.cliques = noticias.cliques + 1
            noticias.save()
            request.session['noticia_cliques_%s' % slugcategoria] = True
    return render_to_response('principal/scope/sub_noticia.html',locals(),context_instance=RequestContext(request))

def ultimas(request):
    noticias = Noticia.objects.all().order_by('-datacriacao').filter(publicar__icontains=1)
    return render_to_response('principal/scope/ultimas.html',locals(),context_instance=RequestContext(request))

def videointerna(request, slugvideo):
    videos = get_object_or_404(Video, slugvideo=slugvideo)
    video = Video.objects.all().order_by('-datacriacao')[:6]
    listaartigo = Noticia.objects.filter(nomeCategoria__id = 5,publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:3]
    listablog = NoticiaBlog.objects.filter(publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:3]

    if videointerna:
        if not request.session.get('videos_cliques_%s' % slugvideo):
            videos.cliques = videos.cliques + 1
            videos.save()
            request.session['videos_cliques_%s' % slugvideo] = True
    return render_to_response('principal/scope/sub_video.html',locals(),context_instance=RequestContext(request))

def videosPrincipal(request):
    videos = Video.objects.all().order_by('-datacriacao')
    listaartigo = Noticia.objects.filter(nomeCategoria__id = 5,publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:3]
    listablog = NoticiaBlog.objects.filter(publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:3]
    return render_to_response('principal/scope/videos.html',locals(),context_instance=RequestContext(request))

def todososblogs(request):
    nomedoblog = Blog.objects.filter(ativo__icontains=1).order_by('-datacriacao')
    return render_to_response('principal/scope/blogs.html',locals(),context_instance=RequestContext(request))

def blog(request, slugdoblog):
    blogsnome = get_object_or_404(Blog, slugdoblog=slugdoblog)
    noticiasdoblog = NoticiaBlog.objects.filter(nomeBlog=blogsnome).order_by('-datacriacao')
    listaartigo = Noticia.objects.filter(nomeCategoria__id = 5,publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:3]
    listablog = NoticiaBlog.objects.filter(publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:3]
    if blog:
        if not request.session.get('blog_cliques_%s' % slugdoblog):
            blogsnome.cliques = blogsnome.cliques + 1
            blogsnome.save()
            request.session['blog_cliques_%s' % slugdoblog] = True
    return render_to_response('principal/scope/blog.html',locals(),context_instance=RequestContext(request))

def blognoticia(request, slugnoticiablog, nomedoblog):
    noticiadoblog = get_object_or_404(NoticiaBlog, slugnoticiablog=slugnoticiablog)
    blognome = get_object_or_404(Blog, slugdoblog = nomedoblog)
    listaartigo = Noticia.objects.filter(nomeCategoria__id = 5,publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:3]
    listablog = NoticiaBlog.objects.filter(publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:3]
    if blognoticia:
        if not request.session.get('noticia_cliques_%s' % slugnoticiablog):
            noticiadoblog.cliques = noticiadoblog.cliques + 1
            noticiadoblog.save()
            request.session['noticia_cliques_%s' % slugnoticiablog] = True
    return render_to_response('principal/scope/blognoticia.html', locals(),context_instance=RequestContext(request))

def evento(request):
    listaevento = Noticia.objects.filter(nomeCategoria__id = 1,publicar__icontains=1).order_by('-datacriacao')
    listaartigo = Noticia.objects.filter(nomeCategoria__id = 5,publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:3]
    listablog = NoticiaBlog.objects.filter(publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:3]
   
    return render_to_response('principal/scope/eventos.html',locals(),context_instance=RequestContext(request))

def artigo(request):
    listaartigo = Noticia.objects.filter(nomeCategoria__id = 5,publicar__icontains=1).order_by('-datacriacao')
    listaartigo = Noticia.objects.filter(nomeCategoria__id = 5,publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:3]
    listablog = NoticiaBlog.objects.filter(publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:3]
   
    return render_to_response('principal/scope/artigos.html',locals(),context_instance=RequestContext(request))

def musica(request):
    listamusica = Noticia.objects.filter(nomeCategoria__id = 4,publicar__icontains=1).order_by('-datacriacao')
    listaartigo = Noticia.objects.filter(nomeCategoria__id = 5,publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:3]
    listablog = NoticiaBlog.objects.filter(publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:3]
   
    return render_to_response('principal/scope/musicas.html',locals(),context_instance=RequestContext(request))

def humor(request):
    listahumor = Noticia.objects.filter(nomeCategoria__id = 2,publicar__icontains=1).order_by('-datacriacao')
    listaartigo = Noticia.objects.filter(nomeCategoria__id = 5,publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:3]
    listablog = NoticiaBlog.objects.filter(publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:3]
   
    return render_to_response('principal/scope/humor.html',locals(),context_instance=RequestContext(request))

def todasasfotos(request):
    listagaleria = Galeria.objects.filter(publicar__icontains=1).order_by('-datacriacao')
    listaartigo = Noticia.objects.filter(nomeCategoria__id = 5,publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:3]
    listablog = NoticiaBlog.objects.filter(publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:3]
   
    return render_to_response('principal/scope/fotos.html',locals(),context_instance=RequestContext(request))

def galeria(request, sluggaleria):
    lista = get_object_or_404(Galeria, sluggaleria=sluggaleria)
    imagens = Imagem.objects.filter(galeriaImagem=lista)
    if galeria:
        if not request.session.get('lista_cliques_%s' % sluggaleria):
            lista.cliques = lista.cliques + 1
            lista.save()
            request.session['lista_cliques_%s' % sluggaleria] = True
    return render_to_response('principal/scope/sub_foto.html',locals(),context_instance=RequestContext(request))

def todososprodutos(request):
    listaprodutos = Produto.objects.all().order_by('-dataentrada')
    return render_to_response('principal/scope/shop.html',locals(),context_instance=RequestContext(request))

def produto(request, slugcategoria, slugproduto):
    lista = get_object_or_404(Produto, nomeCategoria__slugcatproduto = slugcategoria, slugproduto=slugproduto)
    listaartigo = Noticia.objects.filter(nomeCategoria__id = 5,publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:3]
    listablog = NoticiaBlog.objects.filter(publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:3]
    ilhainternaquadrada = Publicidade.objects.filter(ativo__exact='1',tipo__exact='7',dataentrada__lte=datetime.now(),datasaida__gte=datetime.now())[:1]
   
    return render_to_response('principal/scope/sub_shop.html',locals(),context_instance=RequestContext(request))

def busca(request):
    from django.db.models.query_utils import Q
    quante = 0
    if request.GET['buscap']!='' :
        ites = Noticia.objects.filter((Q(subtitulo__icontains = request.GET['buscap'])| Q(titulo__icontains = request.GET['buscap'])))
        if ites:
            quante = ites.count()
        c = Context({'itens_busca': ites, 'quant':quante, 'bus':request.GET['buscap']})
        return render_to_response('principal/scope/busca.html', locals() ,context_instance=RequestContext(request,c))


    return render_to_response('principal/scope/index.html', locals() ,context_instance=RequestContext(request,{'msg':'Por Favor, preencha pelo menos um campo de busca!'}))

def busca(request):
    ### Publicidades
    listaartigo = Noticia.objects.filter(nomeCategoria__id = 5,publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:3]
    listablog = NoticiaBlog.objects.filter(publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:3]
    ilhainterna = Publicidade.objects.filter(ativo__exact='1',tipo__exact='7',dataentrada__lte=datetime.now(),datasaida__gte=datetime.now())[:1]
    ### FimPublicidades
    noticias = Noticia.objects.filter(publicar__exact='True',manchete__exact='0',datacriacao__lte=datetime.now()).order_by('-datacriacao')
    from django.db.models.query_utils import Q
    quante = 0
    if request.GET['buscap']!='':
        ites = Noticia.objects.filter((Q(subtitulo__icontains = request.GET['buscap'])| Q(titulo__icontains = request.GET['buscap'])))
        if ites:
            quante = ites.count()
        c = Context({'itens_busca': ites, 'quant':quante, 'bus':request.GET['buscap']})
        return render_to_response('principal/scope/busca.html', locals() ,context_instance=RequestContext(request,c))


    return render_to_response('principal/scope/index.html', locals() ,context_instance=RequestContext(request,{'msg':'Por Favor, preencha pelo menos um campo de busca!'}))

### Download
def download(request):
    listadownload = Download.objects.filter(publicar__icontains=1).order_by('-datacriacao')
    return render_to_response('principal/download/downloads.html',locals(),context_instance=RequestContext(request))

def downloadcantor(request, slugdocantor):
    puxacantor = get_object_or_404(Cantor, slugcantor=slugdocantor)
    downdocantor = Download.objects.filter(nomeCantor=puxacantor).order_by('-datacriacao')
    return render_to_response('principal/download/downloads_cantor.html',locals(),context_instance=RequestContext(request))

def downloadinterna(request, slugcantor, slugdownload):
    down = get_object_or_404(Download, slugdownload=slugdownload)
    listalink = LinkDownload.objects.filter(linkDownload=down)
    return render_to_response('principal/download/downloads_album.html',locals(),context_instance=RequestContext(request))

def downloadlink(request, iddownload):
    down = get_object_or_404(Download, id=iddownload)
    listalink = LinkDownload.objects.filter(linkDownload=down)
    return render_to_response('principal/download/download_link.html',locals(),context_instance=RequestContext(request))

### Letras
def homeletra(request):
    mancheteletra = Letra.objects.filter(publicar__icontains=1, manchete__icontains=1).order_by('-datacriacao')
    listamusica = Noticia.objects.filter(nomeCategoria__id = 5,publicar__icontains=1, manchete__icontains=0).order_by('-datacriacao')[:4]
    topcantor = Cantor.objects.filter(publicar__icontains=1).order_by('cliques')[:10]
    topletra = Letra.objects.filter(publicar__icontains=1).order_by('cliques')[:10]
    cantor = Cantor.objects.all()
    music = Letra.objects.all()
    return render_to_response('principal/letras/inicial.html',locals(),context_instance=RequestContext(request))

def letracantor(request, slugdocantor):
    cantornome = get_object_or_404(Cantor, slugcantor=slugdocantor)
    manchetecantorletra = Letra.objects.filter(manchete__icontains=1, nomeCantor=cantornome)
    letracantor = Letra.objects.filter(nomeCantor=cantornome)
    topcantor = Cantor.objects.filter(publicar__icontains=1).order_by('cliques')[:10]
    topletra = Letra.objects.filter(publicar__icontains=1).order_by('cliques')[:10]
    return render_to_response('principal/letras/cantor.html',locals(),context_instance=RequestContext(request))

def letra(request, slugdocantor, slugletra):
    letranome = get_object_or_404(Letra, slugletra=slugletra)
    letracantor = Letra.objects.filter(nomeCantor=letranome)
    topcantor = Cantor.objects.filter(publicar__icontains=1).order_by('cliques')[:10]
    topletra = Letra.objects.filter(publicar__icontains=1).order_by('cliques')[:10]
    return render_to_response('principal/letras/letra.html',locals(),context_instance=RequestContext(request))

### Igrejas
def igrejas(request):
    listaigreja = Igreja.objects.all()
    return render_to_response('principal/scope/Igrejas/todasigreja.html',locals(),context_instance=RequestContext(request))

def igreja(request, slugigreja):
    nomedaigreja = get_object_or_404(Igreja, slugigreja=slugigreja)
    listaigreja = Congregacao.objects.filter(nomeIgreja=nomedaigreja)
    return render_to_response('principal/scope/Igrejas/igreja.html',locals(),context_instance=RequestContext(request))

def congregacao(request, slugigreja, slugcongregacao):
    nomedaigreja = get_object_or_404(Igreja, slugigreja=slugigreja)
    congra = get_object_or_404(Congregacao, slugcongregacao=slugcongregacao)
    return render_to_response('principal/scope/Igrejas/congregacao.html',locals(),context_instance=RequestContext(request))

### Biblia
def biblia(request):
    todosvelho = Livro.objects.filter(nomeTestamento_id=1).order_by('posicao')
    todosnovo = Livro.objects.filter(nomeTestamento_id=2).order_by('posicao')
    todasversoes = Versao.objects.all()
    return render_to_response('principal/biblia/inicial.html',locals(),context_instance=RequestContext(request))

def versao(request, slugversao):
    return render_to_response('principal/biblia/versao.html',locals(),context_instance=RequestContext(request))

def livro(request, slugtestamento, slugversao, sluglivro):
    puxalivro = get_object_or_404(Livro, sluglivro=sluglivro)
    capdolivro = Capitulo.objects.filter(nomeLivro=puxalivro)
    todoslivros = Livro.objects.all()
    return render_to_response('principal/biblia/livro.html',locals(),context_instance=RequestContext(request))

def capitulo(request, slugtestamento, slugversao, sluglivro, ncap):
    puxacapitulo = get_object_or_404(Capitulo, ncapitulo=ncap)
    versos = Versiculo.objects.filter(nomeCapitulo=puxacapitulo)
    todoscap = Capitulo.objects.filter(nomeLivro=puxacapitulo)
    return render_to_response('principal/biblia/capitulo.html',locals(),context_instance=RequestContext(request))

def versiculo(request, slugtestamento, slugversao, sluglivro, ncap, versoid):
    puxaversiculo = get_object_or_404(Versiculo, id=versoid)
    return render_to_response('principal/biblia/versiculo.html',locals(),context_instance=RequestContext(request))

def paginanaoencontrada(request):
    noticias = Noticia.objects.all().order_by('-datacriacao').filter(publicar__icontains=1)[:8]
    return render_to_response('principal/scope/404.html',locals(), context_instance=RequestContext(request))